﻿namespace KinonekoSoftware.Studio.Pages.Instances.Characters
{
    public class CharacterEpochSelectorViewModel : ObjectDialog<CharacterEpoch>
    {
        private CharacterEpoch                       _selected;

        public static Task<Result<CharacterEpoch>> Select(ObservableCollection<CharacterEpoch> collection)
        {
            var context = new DialogContext();
            context.Set(ContextBase.Key_Value, collection);
            return Dialog(new CharacterEpochSelectorViewModel(), context);
        }
        
        protected override void CreateValidationRules(ICollection<DialogRule> rules)
        {
            rules.Add(DialogRule.Create<CharacterEpochSelectorViewModel>(CodariaSR.Text_Parameter_EmptyName, x => x.Selected is not null));
        }

        protected override CharacterEpoch OnFinish(bool editing) => Selected;


        protected override bool OnReceive(DialogContext context)
        {
            if (context.Has(ContextBase.Key_Value))
            {
                var collection = context.GetObject<ObservableCollection<CharacterEpoch>>(ContextBase.Key_Value);
                Epochs.AddMany(collection, true);
            }
            return false;
        }


        public CharacterEpoch Selected
        {
            get => _selected;
            set => TryFinishAndSetValue(ref _selected, value);
        }
        
        public ObservableCollection<CharacterEpoch> Epochs { get; } = new ObservableCollection<CharacterEpoch>();

    }
}